﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
#if !WINDOWS_PHONE
using Microsoft.Xna.Framework.Storage;
#endif

namespace Brain.General
{
    /// <summary>
    /// A Helper for loading and saving highscores
    /// </summary>
    public class HighscoresManager
    {
        public string Place { get; protected set; }

        public List<HighscoreData> Data { get; protected set; }

        public event SetGeneric<HighscoresManager> MakeDummyData;

        private int maxHighscores = 30;
        public int MaxHighscores
        {
            get { return maxHighscores; }
            set { maxHighscores = value; }
        }

        public HighscoresManager(string place)
        {
            this.Place = place;
        }

        public virtual void Load()
        {
#if WINDOWS_PHONE
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

            if (file.FileExists(Place))
            {
                using (Stream s = file.OpenFile(Place, FileMode.Open))
                {
                    using (BinaryReader reader = new BinaryReader(s))
                    {
                        int count = reader.ReadInt32();
                        for (int i = 0; i < count; i++)
                        {
                            HighscoreData data = new HighscoreData();
                            data.Score = reader.ReadInt32();
                            data.Name = reader.ReadString();
                            int day = reader.ReadInt32();
                            int month = reader.ReadInt32();
                            int year = reader.ReadInt32();
                            int hour = reader.ReadInt32();
                            int minute = reader.ReadInt32();
                            data.Date = new DateTime(year, month, day, hour, minute, 0);
                            Data.Add(data);
                        }
                    }
                }
            }
            else
            {
                if (MakeDummyData != null)
                {
                    this.Data = new List<HighscoreData>();
                    MakeDummyData(this);
                }
            }

            file.Dispose();
#endif
        }

        public virtual string Save()
        {
            Data.Sort(new HighscoresComparer());
            while (Data.Count > this.MaxHighscores)
            {
                Data.RemoveRange(MaxHighscores, 1);
            }

#if WINDOWS_PHONE
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

            if (file.FileExists(Place))
            {
                file.DeleteFile(Place);
            }

            using (Stream s = file.CreateFile(Place))
            {
                using (BinaryWriter writer = new BinaryWriter(s))
                {
                    Write(writer);
                }
            }

            file.Dispose();

            return null;
#endif
#if WINDOWS || XBOX
            IStorageDeviceManager device = Engine.Instance.StorageDeviceManager;

            if (device.StorageDevice == null)
            {
                return "storagedevicenull";
            }

            
#endif

            return null;
        }

        protected void Write(BinaryWriter writer)
        {
            writer.Write(Data.Count);
            for (int i = 0; i < Data.Count; i++)
            {
                HighscoreData data = Data[i];
                writer.Write(data.Score);
                writer.Write(data.Name);
                writer.Write(data.Date.Day);
                writer.Write(data.Date.Month);
                writer.Write(data.Date.Year);
                writer.Write(data.Date.Hour);
                writer.Write(data.Date.Minute);
            }
        }

    }
}
